Seaborn graphics

Seaborn is a Python library with "a high-level interface for drawing attractive statistical graphics". This notebook includes some examples taken from the Seaborn example gallery.


In [ ]:
# The imports
%matplotlib inline

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style="darkgrid")

Example 1: scatterplot matrix


In [ ]:
import seaborn as sns
sns.set(style="ticks")

df = sns.load_dataset("iris")
sns.pairplot(df, hue="species");

Example 2: Correlation matrix heatmap


In [ ]:
sns.set(context="paper", font="monospace")

# Load the datset of correlations between cortical brain networks
df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0)
corrmat = df.corr()

# Set up the matplotlib figure
f, ax = plt.subplots( figsize=(10, 8) )

# Draw the heatmap using seaborn
sns.heatmap(corrmat, vmax=.8, square=True)

# Use matplotlib directly to emphasize known networks
networks = corrmat.columns.get_level_values("network")
for i, network in enumerate(networks):
    if i and network != networks[i - 1]:
        ax.axhline(len(networks) - i, c="w")
        ax.axvline(i, c="w")
f.tight_layout()

Example 3: Linear regression with marginal distributions


In [ ]:
sns.set(style="darkgrid", color_codes=True)

tips = sns.load_dataset("tips")
g = sns.jointplot("total_bill", "tip", data=tips, kind="reg",
                  xlim=(0, 60), ylim=(0, 12), color="r", height=7)

Interactivity

We repeat the above example, but now using the notebook backend to provide pan & zoom interactivity. Note that this may not work if graphics have already been initialized


In [ ]:
# Seaborn + interactivity
#%matplotlib notebook
%matplotlib widget

In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

sns.set( style="darkgrid", color_codes=True )

tips = sns.load_dataset("tips")
sns.jointplot( "total_bill", "tip", data=tips, kind="reg",
                  xlim=(0, 60), ylim=(0, 12), color="r", height=9 );

In [ ]: